package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.bsg.upm.annotation.Auth;
import com.bsg.upm.constant.UserRoleConstants;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.SoftwareImageService;
import com.bsg.upm.service.SoftwareService;

/**
 * 软件管理控制层
 * 
 * @author HCK
 *
 */
@Auth(value = { UserRoleConstants.ADMIN })
@Controller
@RequestMapping("/v1.0/softwares")
public class SoftwareController {

	@Resource
	private SoftwareService softwareService;

	@Resource
	private SoftwareImageService softwareImageService;

	/**
	 * 【软件查询】
	 * 
	 * @return 操作结果
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listSoftware(HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = softwareService.list();
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【软件镜像查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/images", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listSoftwareImage(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			if (paramMap.containsKey("site")) {
				searchParamMap.put("siteId", paramMap.get("site"));
			}
			respJson = softwareImageService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【软件镜像查询(单条记录)】
	 * 
	 * @param imageId
	 *            软件镜像编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/images/{image}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getSoftwareImage(@PathVariable("image") String imageId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = softwareImageService.get(imageId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【软件镜像新增】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/images", method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveSoftwareImage(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = softwareImageService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【启用】
	 * 
	 * @param imageId
	 *            软件镜像编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/images/{image}/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableSoftwareImage(@PathVariable("image") String imageId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = softwareImageService.enable(imageId, true);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【停用】
	 * 
	 * @param imageId
	 *            软件镜像编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/images/{image}/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableSoftwareImage(@PathVariable("image") String imageId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = softwareImageService.enable(imageId, false);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【软件镜像删除】
	 * 
	 * @param imageId
	 *            软件镜像编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/images/{image}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeSoftwareImage(@PathVariable("image") String imageId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = softwareImageService.remove(imageId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/*************************** add by ycs (start) ***************************/
	@RequestMapping(value = "/images/config-template/{image}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getSoftwareImageKeySets(@PathVariable("image") String imageId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = softwareImageService.getTemplateKeySets(imageId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/images/config-template/{image}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateSoftwareImageKeySets(@PathVariable("image") String imageId,
			@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			List keysets = (List) paramMap.get("key_sets");

			JSONArray keysetJsonArr = new JSONArray();
			for (Object key : keysets) {
				keysetJsonArr.add(key);
			}

			respJson = softwareImageService.updateTemplateKeysets(imageId, keysetJsonArr);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	/*************************** add by ycs (end) ***************************/
}
